//
//  FXCcommentController.m
//  XinLangFeiBo
//
//  Created by dllo on 16/3/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FXCcommentController.h"
#import "FXCTabBarViewController.h"
#import "FXCAccount.h"
#import "FXCNetworking.h"


@interface FXCcommentController ()

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation FXCcommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self creatSubViews];
    
    //添加观察者
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(textFieldTextDidChange:)
     name:UITextFieldTextDidChangeNotification
     object:_textField];
    
}

//移除观察者
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:_textField];
}


- (void)creatSubViews{
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 414, 200)];
    _textField.placeholder = @"写评论....";
    [self.view addSubview:_textField];
    
    UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    
    [sendBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [sendBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:sendBtn];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    [sendBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    sendBtn.enabled = NO;
    _sendBtn = sendBtn;
}

//实现通知方法
-(void)textFieldTextDidChange:(NSNotification *)notification
{
    UITextField *textfield=[notification object];
    _sendBtn.enabled = textfield.text.length != 0;
    
}


- (void)sendMessage{
    NSString *url = @"https:/open.weibo.cn/2/comments/create.json";
    
    NSDictionary *dic = @{@"comment":_textField.text,@"id":[NSNumber numberWithLongLong:_Id]};
    [FXCNetworking postWithURL:url parameter:dic success:^(id responseObject) {
        //        添加提示窗
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.labelText = @"评论完成";
        //        延迟处理
        
        [hud hide:YES afterDelay:1];
        [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0];
    } failure:^(NSError *error) {
        NSLog(@"%@" ,error);
    }];
    
    
}
- (void)delayMethod{
    [self.navigationController popViewControllerAnimated:YES];
}





@end
